//
//  DeviceCyclingRecordVC.m
//  KEREN
//
//  Created by MW on 2023/4/6.
//

#import "DeviceCyclingRecordVC.h"
#import "CyclingstatisticsVC.h"
#import "TrackListVC.h"
@interface DeviceCyclingRecordVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) UIView *currentLineView;
@end

@implementation DeviceCyclingRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"" navColorIswhite:YES withTitle:@"骑行记录"];
    
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60 + STATUSBAR_HEIGHT);
        make.height.offset(48);
        make.left.right.offset(0);
    }];
    topView.backgroundColor = Whire_Color;
    DeviceInfo *deviceInfo = [UserInfoManager sharedInstance].currentDeviceInfo;
    NSMutableArray *titleArr = @[].mutableCopy;
    if(deviceInfo.deviceConfig.ride_statistics){
        [titleArr addObject:@"骑行统计"];
    }
    if(deviceInfo.deviceConfig.track_list){
        [titleArr addObject:@"轨迹列表"];
    }
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/(titleArr.count) * i, 0, DEVICE_WIDTH/(titleArr.count), 47)];
        [topView addSubview:btn];
        if(i == 0){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            _currentLineView = [[UIView alloc] init];
            [topView addSubview:_currentLineView];
            [_currentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(64);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
            _currentLineView.backgroundColor = Color_7ACC00;
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:btn];
    }
    [UIView setLineViewWithframe:CGRectMake(0, 47, DEVICE_WIDTH, 1) forinView:topView];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT)];
    [self.view addSubview:_scrollView];
    
    if(titleArr.count == 2){
        CyclingstatisticsVC *csVC = [[CyclingstatisticsVC alloc] init];
        [self addChildViewController:csVC];
        csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
        
        TrackListVC *tlVC = [[TrackListVC alloc] init];
        [self addChildViewController:tlVC];
        tlVC.view.frame = CGRectMake(DEVICE_WIDTH, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
        
        [_scrollView addSubview:csVC.view];
        [_scrollView addSubview:tlVC.view];
    }else if(titleArr.count == 1){
        if(deviceInfo.deviceConfig.ride_statistics){
            CyclingstatisticsVC *csVC = [[CyclingstatisticsVC alloc] init];
            [self addChildViewController:csVC];
            csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
            [_scrollView addSubview:csVC.view];
        }else if(deviceInfo.deviceConfig.track_list){
            TrackListVC *tlVC = [[TrackListVC alloc] init];
            [self addChildViewController:tlVC];
            tlVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
            [_scrollView addSubview:tlVC.view];
        }
    }
   
    
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(DEVICE_WIDTH * titleArr.count, 0);
    _scrollView.delegate = self;
    
    _scrollView.scrollEnabled = titleArr.count > 1 ? YES : NO;
}

- (void)btnClick:(UIButton *)sender{
    [self updateTopView:sender.tag];
    [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * sender.tag, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x = scrollView.contentOffset.x + DEVICE_WIDTH /2;
    int index = x / DEVICE_WIDTH ;
    [self updateTopView:index];
}

- (void)updateTopView:(NSInteger)tag{
    for (UIButton *btn in _btnArr) {
        if(btn.tag == tag){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            [_currentLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(64);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
    }
}

- (NSMutableArray *)btnArr{
    if(!_btnArr){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

@end
